/**
 * 
 */
package neu.edu.vn.appmusic.adapter;

import java.util.ArrayList;
import java.util.List;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import neu.edu.vn.appmusic.R;
import neu.edu.vn.appmusic.object.Song;
import neu.edu.vn.appmusic.utils.ImageUtils;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author Hungvm.qn
 *
 */
public class SongAdapter extends ArrayAdapter {
	private ArrayList<Song> listSong;
	private Context mContext;
	private int layoutId;
	
	public SongAdapter(Context context, int resource, ArrayList<Song> objects) {
		super(context, resource, objects);
		listSong = objects;
		mContext = context;
		layoutId = resource;

	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		
		if(convertView == null){
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView =  inflater.inflate(layoutId, null);
			viewHolder = new ViewHolder();
			//findViewById
			viewHolder.ivSong = (ImageView) convertView.findViewById(R.id.ivSongImageItem);
			viewHolder.tvSongName = (TextView) convertView.findViewById(R.id.tvSongNameItem);
			viewHolder.tvSongAuthor = (TextView) convertView.findViewById(R.id.tvSongAuthorItem);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		Song song = listSong.get(position);
		if(song != null){
			//add data form song to viewHolder
			viewHolder.tvSongAuthor.setText(song.getAuthor());
			viewHolder.tvSongName.setText(song.getSongName());
			ImageUtils.getInstance(mContext).loadImageToImageview(song.getUrlImage(), viewHolder.ivSong);
//			viewHolder.ivSong.setImageResource(R.drawable.default_music);
			
		}
		return convertView;
	}
	
	static class ViewHolder{
		ImageView ivSong;
		TextView tvSongName;
		TextView tvSongAuthor;
	}
		
}
